package com.hmsproject.socialAuth;

import java.io.Serializable;
import java.util.Map;
import java.util.Properties;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.brickred.socialauth.AuthProvider;
import org.brickred.socialauth.Profile;
import org.brickred.socialauth.SocialAuthConfig;
import org.brickred.socialauth.SocialAuthManager;
import org.brickred.socialauth.util.SocialAuthUtil;

@ManagedBean
@SessionScoped
public class FacebookBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SocialAuthManager manager;
	private Profile profile = new Profile();
	public SocialAuthManager getManager() {
		return manager;
	}

	public void setManager(SocialAuthManager manager) {
		this.manager = manager;
	}

	public Profile getProfile() {
		return profile;
	}

	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	String FACEBOOK_APP_ID = "401532843215071";
	String FACEBOOK_APP_SECRET = "1773c2fe4a434b4723077f104b226117";

	public String pullUserInfo() {

		try {
			// Pull user's data from the provider

			ExternalContext externalContext = FacesContext.getCurrentInstance()
					.getExternalContext();
			HttpServletRequest request = (HttpServletRequest) externalContext
					.getRequest();

			if (manager != null) {

				Map<String, String> paramsMap = SocialAuthUtil
						.getRequestParametersMap(request);
				AuthProvider provider = manager.connect(paramsMap);
				profile = provider.getUserProfile();
				// Do what you want with the data (e.g. persist to the database,
				// etc.)
				System.out.println("User's Social profile: " + profile
						+ " LIST " + provider.getContactList().toString());
			}
		} catch (Exception ex) {
			System.out.println("Excetpion " + ex.toString());
		}
		return profile.toString();

	}

	public void socialConnect() throws Exception {
		// Put your keys and secrets from the providers here
		Properties props = System.getProperties();
		props.put("graph.facebook.com.consumer_key", FACEBOOK_APP_ID);
		props.put("graph.facebook.com.consumer_secret", FACEBOOK_APP_SECRET);
		props.put("graph.facebook.com.custom_permissions",
				"publish_stream,email,user_birthday,user_location,offline_access");

		SocialAuthConfig config = SocialAuthConfig.getDefault();

		config.load(props);
		manager = new SocialAuthManager();
		manager.setSocialAuthConfig(config);
		FacesContext context = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) context.getExternalContext()
				.getSession(true);
		String successURL = "http://localhost:8080/hms/" +";/";
		String authenticationURL = manager.getAuthenticationUrl("facebook",
				successURL);

		session.setAttribute("authManager", manager);
		
	
		FacesContext.getCurrentInstance().getExternalContext()
				.redirect(authenticationURL);
	}
}
